import {defineStore} from 'pinia'
import { ref } from "vue";
import { flyroleGetApi, updateUserInfoApi, userLoginApi } from '../api/user';
import router from '../router';

export const useAccount = defineStore('account',()=>{
    const userInfo = ref(null)
    const jwttoken = ref('')
    const permitData = ref(null)
    const userLogin = async(formData)=>{
        // 发送登录请求
        const res = await userLoginApi(formData)
        let {code,token,data} = res.data
        jwttoken.value = token
        userInfo.value = data
        console.log('发送网络请求',res);
        if(code!=0){
            let permit = await flyroleGetApi(data.flyroleId)
            console.log('角色的权限信息',permit);
            permitData.value = permit.data.data[0]
        }
        return res
    }
    // 更新用户信息
    const userUpdate = async(newData) => {
        userInfo.value = { ...userInfo.value, ...newData }
        let permit = await flyroleGetApi(newData.flyroleId)
        console.log('角色的权限信息',permit);
        permitData.value = permit.data.data[0]
    }
    // 更新用户
    const updateUserInfo = async (jwttoken,_id,payload)=>{
        console.log('111',_id);
        let res = await updateUserInfoApi(jwttoken,_id,payload)
        console.log(res);
    }
    return {
        userInfo, 
        jwttoken,
        permitData,
        userLogin,
        userUpdate,
        updateUserInfo,
    }
},{
    persist:{
        key:"fly-project-yangmin",
        enabled:true,
    }
})